package com.qf.web.controller;

import com.qf.pojo.BookInfo;
import com.qf.pojo.ClassInfo;
import com.qf.service.BookInfoService;
import com.qf.service.ClassInfoService;
import com.qf.service.LendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * 书籍
 */
@Slf4j
@Controller
@RequestMapping("/bookinfo")
public class BookController {

    @Autowired
    private BookInfoService bookInfoService;
    @Autowired
    private ClassInfoService classInfoService;
    @Autowired
    private LendService lendService;

    /**
     * 查询书籍
     * @return
     */
    @GetMapping("/list")
    public ModelAndView list(){
        log.info("BookController list ......");
        List<BookInfo> list = bookInfoService.findAll();
        ModelAndView modelAndView = new ModelAndView("bookinfo-list");
        modelAndView.addObject("bookInfo",list);
        return modelAndView;
    }

    /**
     * 查询书籍(借阅方
     * @return
     */
    @GetMapping("/readerList")
    public ModelAndView readerList(){
        log.info("BookController readerList ......");
        List<BookInfo> list = bookInfoService.findAll();
        ModelAndView modelAndView = new ModelAndView("reader_books");
        modelAndView.addObject("books",list);
        return modelAndView;
    }

    /**
     * 查询书籍详细信息
     * @param bookId
     * @return
     */
    @GetMapping("/detail.html")
    public ModelAndView detail(Long bookId){
        log.info("BookController detail ......");
        BookInfo bookInfo = bookInfoService.findByIdWithClass(bookId);
        ModelAndView modelAndView = new ModelAndView("bookinfo-detail");
        modelAndView.addObject("bookInfo",bookInfo);
        return modelAndView;
    }
    /**
     * 查询书籍详细信息(借阅方
     * @param bookId
     * @return
     */
    @GetMapping("/readerdetail")
    public ModelAndView readerdetail(Long bookId){
        log.info("BookController readerdetail ......");
        BookInfo bookInfo = bookInfoService.findByIdWithClass(bookId);
        ModelAndView modelAndView = new ModelAndView("reader_detail");
        modelAndView.addObject("detail",bookInfo);
        return modelAndView;
    }

    /**
     * 通过关键字查询所需书籍
     * @param searchWord
     * @return
     */
    @PostMapping("/search")
    public ModelAndView search(String searchWord){
        log.info("BookController search ......");
        List<BookInfo> list = bookInfoService.findByWord(searchWord);
        ModelAndView modelAndView = new ModelAndView("bookinfo-list");
        modelAndView.addObject("bookInfo",list);
        modelAndView.addObject("searchWord",searchWord);
        return modelAndView;
    }

    /**
     * 通过关键字查询所需书籍（借阅方
     * @param searchWord
     * @return
     */
    @PostMapping("/readersearch")
    public ModelAndView readersearch(String searchWord){
        log.info("BookController readersearch ......");
        List<BookInfo> list = bookInfoService.findByWord(searchWord);
        ModelAndView modelAndView = new ModelAndView("reader_books");
        modelAndView.addObject("books",list);
        modelAndView.addObject("searchWord",searchWord);
        return modelAndView;
    }

    /**
     * 删除书籍
     * @param bookId
     * @return
     */
    @GetMapping("/delete.html")
    public ModelAndView delete(Long bookId){
        log.info("BookController delete ......");
        bookInfoService.deleteByBookId(bookId);
        ModelAndView modelAndView = new ModelAndView("redirect:/bookinfo/list");
        modelAndView.addObject("succ","删除成功");
        return modelAndView;
    }

    /**
     * 跳转到添加页面
     * @param bookId
     * @return
     */
    @GetMapping("/addui")
    public ModelAndView addUI(Long bookId){
        log.info("BookController addui ......");
        List<ClassInfo> list = classInfoService.findAll();
        ModelAndView modelAndView = new ModelAndView("bookinfo-add");
        modelAndView.addObject("classInfo",list);

        return modelAndView;
    }

    /**
     * 添加书籍
     * @param bookInfo
     * @return
     */
    @PostMapping("/add")
    public ModelAndView add(BookInfo bookInfo){
        log.info("BookController add ......");
        bookInfoService.addBook(bookInfo);
        ModelAndView modelAndView = new ModelAndView("redirect:/bookinfo/list");
        return modelAndView;
    }

    /**
     * 跳转到修改页面
     * @param bookId
     * @return
     */
    @GetMapping("/update.html")
    public ModelAndView updateUI(Long bookId){
        log.info("BookController updateUI ......");
        Map<String,Object> map = bookInfoService.findById(bookId);
        ModelAndView modelAndView = new ModelAndView("bookinfo-update");
        modelAndView.addObject("bookInfo",map.get("bookInfo"));
        modelAndView.addObject("classInfo",map.get("classInfo"));
        return modelAndView;
    }

    /**
     * 修改书籍信息
     * @param bookInfo
     * @return
     */
    @PostMapping("/update")
    public ModelAndView update(BookInfo bookInfo){
        log.info("BookController update ......");
        ModelAndView modelAndView = new ModelAndView("redirect:/bookinfo/list");

        bookInfoService.updateBookInfo(bookInfo);
        modelAndView.addObject("succ","更新成功");

        return modelAndView;
    }

    /**
     * 借书
     * @param bookId
     * @return
     */
    @GetMapping("/update/number.html")
    public ModelAndView updateNumber(Long bookId){
        log.info("BookController updateUI ......");
        bookInfoService.updateBookInfoByBookId(bookId);
        ModelAndView modelAndView = new ModelAndView("redirect:/bookinfo/list");
        modelAndView.addObject("succ","更新成功");
        return modelAndView;
    }

    /**
     * 借书(借阅方
     * @param bookId
     * @return
     */
    @GetMapping("/lendbook.html")
    public ModelAndView lendbook(Long bookId,Long readerId){
        log.info("BookController updateUI ......");

        lendService.insert(bookId,readerId);
        bookInfoService.updateBookInfoByBookId(bookId);
        ModelAndView modelAndView = new ModelAndView("redirect:/bookinfo/readerList");
        modelAndView.addObject("succ","更新成功");
        return modelAndView;
    }

}
